<?php
namespace app\admin\controller;
header("content-type:text/html;charset=utf-8");
use app\admin\controller\Base;
use app\admin\model\Apply;
use app\admin\model\Applytype;
use app\admin\model\Course;
use app\admin\model\Dustbin;
use app\admin\model\Nation;
use think\App;
use think\Request;
use app\admin\model\User;
use think\facade\Db;
class  Dustbins extends Base
{
    //删除回收站的单条数据
    public function deletes(Request $request)
    {
        $user = $this->user_info();
        if (Dustbin::destroy($request->post('id'))) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }
    //搜索学员
    public function search(){
        //查询所有的报名状态
        $data = array(
            "setting" => $this->getAllSetting(),
            "status" => Applytype::order("sort asc")->select(),
            "course" => Course::select(),
            "nation" => Nation::select()
        );
        return view("search",$data);
    }
    //去搜索学员
    public function toSearch(Request $request){
        $user = $this->user_info();
        //接收搜索条件
        $data = $request->post();
        //组织条件
        $whereString  = [];
        //学生姓名
        if (!empty($data["name"] )){
            $whereString['name'] = ['like',"%{$data['name']}%"];
        }
        //年级
        if (!empty($data["grade"] )){
            $whereString['grade'] = ['like',"%{$data['grade']}%"];
        }
        //性别
        if ($data["sex"] != ""){
            $whereString['sex'] = $data["sex"];
        }
        //学生电话
        if (!empty($data["phone"])){
            $whereString['phone'] = ['like',"%{$data['phone']}%"];
        }
        //身份证
        if (!empty($data["idcards"])){
            $whereString['idcard'] = ['like',"%{$data['idcards']}%"];
        }
        //父母电话
        if (!empty($data["parenttel"])){
            $whereString['parenttel'] = ['like',"%{$data['parenttel']}%"];
        }
        //民族
        if (!empty($data["nation"])){
            $whereString['nation'] = ['like',"%{$data['nation']}%"];

        }
        //负责老师
        if (!empty($data["teacher"])){
            $whereString['teacher'] = ['like',"%{$data['teacher']}%"];

        }
        //备注
        if (!empty($data["comment"])){
            $whereString['comment'] = ['like',"%{$data['comment']}%"];
        }
        //组织sql语句
        $list = Dustbin::where($whereString)->order("del_time","desc")->select();
        //将数据组织为表格行
        $str = "";
        if(!empty($list)) {
            foreach($list as $v){
                $update_time = strtotime($v['apply_update_time']) == 0 ? "未修改" : $v['apply_update_time'];
                $sex = $v['sex']?"男":"女";
                $cz =  "<a class='refresh' title='恢复' data-id=\"{$v['id']}\" data-name=\"{$v['name']}\"><i class=\"fa fa-refresh\" style=\"color:#19a9d5\"></i></a> <a class=\"del\" title='彻底删除' data-id=\"{$v['id']}\" data-name=\"{$v['name']}\"><i class=\"fa fa-trash\" style=\"color:#cacfd3\"></i></a>";
                $status = "";
                switch($v['status']){
                    case 1 : $status = "缴费等审核";break;
                    case 2 : $status = "已录取";break;
                    case 3 : $status = "报名拒绝";break;
                    case 4 : $status = "待支付";break;
                    case 5 : $status = "报名信息审核中";break;
                }
                $user_update_time =  $v['user_update_time'] ? date('Y-m-d h:i:s',$v['user_update_time']) : "未修改";
                $pay_time = $v['pay_time'] ? date('Y-m-d h:i:s',$v['pay_time']) : "未支付";
                $str .= "
          <tr>
              <td>{$v['id']}</td>`
              <td>{$v['name']}</td>
              <td>{$v['grade']}</td>
              <td>{$sex}</td>
              <td>{$v['phone']}</td>
              <td>{$v['parenttel']}</td>
              <td>{$v['bktype']}</td>
              <td>{$v['coursetype_name']}</td>
              <td>{$v['major']}</td>
              <td>{$v['major2']}</td>
              <td>{$v['isadjust']}</td>
              <td>{$v['teacher']}</td>
              <td>".date('Y-m-d h:i:s',$v['del_time'])."</td>
               <td>{$v['del_comment']}</td>
              <td class=\"tools\">
                  ".$cz."
              </td>
               <!--隐藏起来的列表信息-->
              <td>{$v['nation']}</td>
              <td>{$v['qqwx']}</td>
              <td>{$v['schooladdress']}</td>
              <td>{$v['schoolname']}</td>
              <td>{$v['score']}</td>
              <td>{$v['idcard']}</td>
              <td>{$v['comment']}</td>
              <td>{$v['address']}</td>
              <td>".date('Y-m-d h:i:s',$v['user_create_time'])."</td>
              <td>{$user_update_time}</td>
              <td>{$status}</td>
              <td>".date('Y-m-d h:i:s',$v['apply_create_time'])."</td>
              <td>{$update_time}</td>
              <td>{$v['paytype']}</td>
              <td>{$v['payid']}</td>
              <td>{$v['type']}</td>
              <td>{$pay_time}</td>
              <td>{$v['teacher_comment']}</td>
          </tr>";
            }
            $this->success($str);
        }else{
            $this->success("<tr><td colspan=''>未查到数据</td></tr>");
        }

    }
    //恢复数据
    public function refresh(Request $request){
        //接收数据
        $data = $request->post();
        //查询要恢复的数据
        $result = Dustbin::get($data['id']);
        //首先恢复用户数据到user表，然后在恢复报名信息到apply表
        $user = [
            "name" => $result['name'], //姓名
            "pwd" => $result['pwd'], //密码
            "sex" => $result['sex'], //性别
            "phone" => $result['phone'], //联系电话
            "idcard" => $result['idcard'], //身份证号码
            "create_time" => $result['user_create_time'], //创建时间
            "update_time" => $result['user_update_time'], //修改时间
            "nation" => $result['nation'], //民族
            "qqwx" => $result['qqwx'], //qq/微信
            "parenttel" => $result['parenttel'], //父母手机号
            "schooladdress" => $result['schooladdress'], //毕业学校所在地
            "schoolname" => $result['schoolname'], //学校名称
            "score"  => $result['score'], //中考（模拟）成绩
            "address"  => $result['address'], //通讯地址
            "grade" => $result['grade'], //年级
        ];
        //恢复User数据表数据
        $refresh_user_result = User::create($user);
        if($refresh_user_result){
            //如果回复User数据表（用户注册信息）数据成功则回复apply表数据（报名信息）
            $apply = [
                "userid" => $refresh_user_result["id"], //关联user表id
                "major" => $result["major"], //专业
                "bktype" => $result["bktype"], //报考类别
                "create_time" => $result["apply_create_time"], //创建时间
                "update_time" => $result["apply_update_time"], //修改时间
                "reason" => $result["reason"], //拒绝理由
                "comment" => $result["comment"], //备注
                "status" => $result["status"], //状态
                "paytype" => $result["paytype"], //支付类型
                "payid" => $result["payid"], //支付账号（名称）
                "type" => $result["type"], //订单类型
                "pay_time" => $result["pay_time"], //支付时间
                "major2" => $result["major2"], //专业2
                "isadjust" => $result["isadjust"], //是否服从调剂：1是 0否
                "teacher" => $result["teacher"], //报名老师
                "advice_note" => $result["advice_note"], //通知书编号
                "grade" => $result["grade"], //入学年份
                "teacher_comment" => $result["teacher_comment"], //老师备注
                "teacher_id" => $result["teacher_id"], //老师id
                "major_id" => $result["major_id"], //专业1id,关联专业表course
                "major2_id" => $result["major2_id"], //专业2id，关联专业表course
                "bktype_id" => $result["bktype_id"], //报考类型id,关联examtype
                "coursetype_name" => $result["coursetype_name"], //专业大类名称
                "coursetype_id" => $result["coursetype_id"], //专业大类id
            ];
            $apply_refresh_result = Apply::create($apply);
            if($apply_refresh_result){
                //成功恢复数据后删除回收站的数据
                Dustbin::destroy($result['id']);
                return $this->success("以恢复了\"{$result['name']}\"的数据");
            }else{
                return $this->error("恢复\"{$result['name']}\"的报名信息");
            }
        }else{
            return $this->error("恢复\"{$result['name']}\"的注册信息失败！");
        }

    }
    //清空回收站
    public function empty_dustbin(){
        \think\Db::query("delete from dustbin");
       \think\Db::query("alter table dustbin  auto_increment=1");
       $this->success("回收站已清空","Dustbins/search");

    }
}